package com.yuandian.customer.perception.controller;

import com.yuandian.api.customer_perception.po.AddExportListParams;
import com.yuandian.api.customer_perception.po.BusinessOverviewParams;
import com.yuandian.api.customer_perception.po.LowFrequencyOverviewParams;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.customer.perception.service.ILowFrequencyOverviewService;
import com.yuandian.utils.PageDataInfo;
import com.yuandian.utils.YDateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.*;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 说明：低频受理业务概况controller
 * @Author yanjun
 * @Date 2021年4月14日 上午9:15:52
 */
@RestController
@RequestMapping("/lowfrequency/overview")
public class LowFrequencyOverviewController {

	@Autowired
	private ILowFrequencyOverviewService lowFrequencyOverviewService;

	/**
	 * 获取时间周期
	 * @return 通用返回对象
	 */
	@RequestMapping(value = "/queryCycleTime", method = RequestMethod.POST)
	public R<Object> queryCycleTime(@RequestBody LowFrequencyOverviewParams params) {

		Map<String, Object> map = new HashMap<>();
		LocalDateTime type1DateTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN).with(DayOfWeek.MONDAY);
		LocalDateTime type2DateTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN).with(TemporalAdjusters.firstDayOfMonth());
		LocalDateTime type3DateTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN).with(TemporalAdjusters.firstDayOfMonth());
		Instant type1Instant = type1DateTime.toInstant(ZoneOffset.of("+8"));
		Instant type2Instant = type2DateTime.toInstant(ZoneOffset.of("+8"));
		Instant type3Instant = type3DateTime.toInstant(ZoneOffset.of("+8"));
		Date date1 = Date.from(type1Instant);
		Date date2 = Date.from(type2Instant);
		Date date3 = Date.from(type3Instant);
		long oneDayUsec = 24 * 60 * 60 * 1000;
		map.put("type1EndTime", YDateUtil.yyyy_MM_dd(new Date(date1.getTime() - oneDayUsec)));
		map.put("type2EndTime", YDateUtil.yyyy_MM_dd(new Date(date2.getTime() - oneDayUsec)));
		map.put("type3EndTime", YDateUtil.yyyy_MM_dd(new Date(date3.getTime() - oneDayUsec)));
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date1);
		calendar.add(Calendar.DAY_OF_MONTH, -7);
		map.put("type1StartTime", YDateUtil.yyyy_MM_dd(calendar.getTime()));
		calendar.setTime(date2);
		calendar.add(Calendar.MONTH, -1);
		map.put("type2StartTime", YDateUtil.yyyy_MM_dd(calendar.getTime()));
		calendar.setTime(date3);
		calendar.add(Calendar.MONTH, -3);
		map.put("type3StartTime", YDateUtil.yyyy_MM_dd(calendar.getTime()));
		return R.ok(map);
	}

	/**
	 * 查询低频受理业务概况列表
	 * @param params 低频受理业务概况请求对象
	 * @return 通用分页对象
	 */
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public R<Object> list(@RequestBody LowFrequencyOverviewParams params) {
		PageDataInfo info = lowFrequencyOverviewService.queryLowFrequencyOverviewList(params);
		return R.ok(info);
	}

	/**
	 * 添加至导出列表
	 * @param params 导出列表vo对象集合
	 * @return 通用返回对象
	 */
	@RequestMapping(value = "/addExportList", method = RequestMethod.POST)
	public R addExportList(@RequestBody AddExportListParams params) {
		return lowFrequencyOverviewService.addExportList(params);
	}

	/**
	 * 一次性全部页数的数据 添加至导出列表
	 * @param params 导出列表vo对象集合
	 * @return 通用返回对象
	 */
	@RequestMapping(value = "/addAllExportList", method = RequestMethod.POST)
	public R addAllExportList(@RequestBody LowFrequencyOverviewParams params) {
		return lowFrequencyOverviewService.addAllExportList(params);
	}

	/**
	 * 业务概况-树形图
	 * @param params 低频受理-业务概况请求对象
	 * @return 通用返回对象
	 */
	@RequestMapping(value = "/tree", method = RequestMethod.POST)
	public R tree(@RequestBody BusinessOverviewParams params) {
		return lowFrequencyOverviewService.tree(params);
	}
}
